package com.project.medical.sys.filter;


import com.project.medical.sys.service.ManagerService;
import com.project.medical.sys.util.Encoder;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

@Component
public class LoginFilter implements Filter {
    @Autowired
    ManagerService managerService;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse res = (HttpServletResponse) response;
        HttpServletRequest req = (HttpServletRequest) request;
        String origin = req.getHeader("Origin");

        if (StringUtils.isNotEmpty(origin)) {
            //带cookie的时候，origin必须是全匹配，不能使用*
            res.addHeader("Access-Control-Allow-Origin", origin);
        }
        res.addHeader("Access-Control-Allow-Methods", "*");

        String headers = req.getHeader("Access-Control-Request-Headers");
        // 支持所有自定义头
        if (StringUtils.isNotEmpty(headers)) {
            res.addHeader("Access-Control-Allow-Headers", headers);
        }
        res.addHeader("Access-Control-Max-Age", "3600");
        // enable cookie
        res.addHeader("Access-Control-Allow-Credentials", "true");

        //处理options请求
        StringBuffer url = req.getRequestURL();
        if (req.getMethod().toUpperCase().equals("OPTIONS")){
            return;
        }
       else if (url.toString().contains("http://localhost:3000/sys/login") || url.toString().equals("http://localhost:3000/sys/book/list") || url.toString().equals("http://localhost:3000/sys/reader/save")){

           filterChain.doFilter(req,res);
        }

       else {

            //获取token
            //String auth = req.getHeader("Authorization");
            String token = req.getHeader("token");
            System.out.println(token);
            if  (token==null || "".equals(token) || token.contains("undefined")){
                ((HttpServletResponse) response).sendRedirect("/sys/login/unLogin");
                return;
            }
            String[] split = token.split(",");
            String[] passwordSplit = split[1].split(":");
            String password = passwordSplit[1].substring(1,passwordSplit[1].length()-1);
            String[] typeSplit = split[2].split(":");
            String type = typeSplit[1].substring(1, typeSplit[1].length()-2);
            String[] phoneSplit = split[0].split(":");
            String phone = phoneSplit[1].substring(1, phoneSplit[1].length()-1);
            HashMap<String, Object> map = new HashMap<>();
            System.out.println("before"+password+" "+phone+" "+type);
            map.put("phone",phone);
            map.put("password",Encoder.encoder(password));
            System.out.println("after"+map.get("password"));
            boolean checkToken;
            checkToken = managerService.checkToken(map);
//            if (type.equals("admin")){
//              checkToken = managerService.checkToken(map);
//            }else {
//                checkToken = readerService.checkToken(map);
//            }
            if (!checkToken){
                ((HttpServletResponse) response).sendRedirect("/sys/login/unLogin");
                return;
            }else {
                filterChain.doFilter(req,res);
            }

        }





    }

    @Override
    public void destroy() {

    }
}
